﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class QuanTri_TrangDanhSachLoiHS : System.Web.UI.Page
{
    SoLienLacDataContext db = new SoLienLacDataContext();
    clsLoadCombobox cls = new clsLoadCombobox();
    string mahs;
    string tenhs;
    string manam;
    string tennam;
    string tenlop;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null || Session["Username"].ToString() == "")
        {
            Response.Redirect("~/GiaoDienClient/TrangChu.aspx");
        }
        else
        {
            mahs = Request.QueryString["MaHS"];
            var c = from p in db.HocSinhs where p.MaHS == mahs select p.TenHS;
            tenhs = c.First().ToString();
            manam = Request.QueryString["MaNam"];
            tennam = Request.QueryString["TenNam"];
            if (!IsPostBack)
            {
                lblMaHS.Text = mahs;
                lblMaNam.Text = manam;
                lblNamHoc.Text = tennam;
                lblTenHS.Text = tenhs;
                cls.LoadComLoiVP(drLoiVP);
                LoadLoiVP(int.Parse(manam), mahs);
                if (rdoCoPhep.Checked == true || rdoKhongPhep.Checked == true)
                { drLoiVP.Enabled = false; }
                else drLoiVP.Enabled = true;

            }
        }

    }
    private void LoadLoiVP(int manam, string mahs)
    {
        var c = from p in db.ViPhams
                where p.MaNam == manam && p.MaHS == mahs
                select new { p.MaLoiVP, p.LoiViPham.TenLoiVP, p.NgayVP, p.SoLanVP };
        grvLoiVP.DataSource = c;
        grvLoiVP.DataBind();
    }
    private void ThemLoiVP()
    {
        ViPham vp = new ViPham();
        vp.MaNam =int.Parse( manam);
        vp.MaHS = mahs;
        vp.NgayVP = DateTime.Parse(txtNgayVP.Text);
        if (rdoCoPhep.Checked == true)
        { vp.MaLoiVP = 1; vp.SoLanVP = 1; }
        if (rdoKhongPhep.Checked == true)
        { vp.MaLoiVP = 2; vp.SoLanVP = 1; }
        if (rdoCoPhep.Checked == false && rdoKhongPhep.Checked == false)
        { vp.MaLoiVP = int.Parse(drLoiVP.SelectedValue.ToString());
        vp.SoLanVP = int.Parse(txtSoLan.Text);
        }
        if (rdoKyI.Checked == true)
        { vp.MaKy = 1; }
        if (rdoKyII.Checked == true)
        { vp.MaKy = 2; }
        
        db.ViPhams.InsertOnSubmit(vp);
        db.SubmitChanges();
        rdoCoPhep.Checked = false;
        rdoKhongPhep.Checked = false;
        rdoKyI.Checked = false;
        rdoKyII.Checked = false;

    }
    protected void btnThemLoiVP_Click(object sender, EventArgs e)
    {
        ThemLoiVP();
        LoadLoiVP(int.Parse(manam), mahs);
    }
    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        //if(rd)
    }
    protected void rdoKhongPhep_CheckedChanged(object sender, EventArgs e)
    {

    }
    protected void rdoCoPhep_CheckedChanged(object sender, EventArgs e)
    {
        if (rdoCoPhep.Checked == true)
        {
            drLoiVP.Enabled = false;
        }
        if (rdoCoPhep.Checked == false)
        {
            drLoiVP.Enabled = true;
        }
    }
    protected void imbBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("TrangDanhSachHS.aspx");
    }
    protected void grvLoiVP_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void grvLoiVP_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int hangchon = e.RowIndex;
        //grvLoiVP.DeleteRow(hangchon);
        Label lblMaVP = grvLoiVP.Rows[hangchon].FindControl("lblMaLoiVP")as Label;
        Label lblNgayVP = grvLoiVP.Rows[hangchon].FindControl("lblNgayVP") as Label;
        ViPham vp = db.ViPhams.SingleOrDefault(p=>p.MaHS==mahs && p.MaLoiVP==int.Parse(lblMaVP.Text)&& p.MaNam==int.Parse(manam)&&p.NgayVP==DateTime.Parse(lblNgayVP.Text));
        db.ViPhams.DeleteOnSubmit(vp);
        db.SubmitChanges();
        LoadLoiVP(int.Parse(manam),mahs);
    }
}